class_name SmallTree
extends Node2D


@onready var hurt_component: HurtComponent = $HurtComponent
@onready var damage_component: DamageComponent = $DamageComponent
@onready var sprite: Sprite2D = $Sprite2D

var log_scene: PackedScene = preload("res://scenes/objects/trees/log.tscn")

func _ready() -> void:
	hurt_component.hurt.connect(on_hurt)
	damage_component.max_damage_reached.connect(on_max_damage_reached)


func on_hurt(damage: int) -> void:
	damage_component.apply_damage(damage)
	do_shake()


func on_max_damage_reached() -> void:
	call_deferred("add_log_scene")
	print("on_max_damage_reached")
	queue_free()

	
func add_log_scene() -> void:
	var log_instance: Node2D = log_scene.instantiate() as Node2D
	log_instance.global_position = global_position
	get_parent().add_child(log_instance)


func do_shake() -> void:
	var mat: ShaderMaterial = sprite.get_material() as ShaderMaterial
	mat.set_shader_parameter("shake_intensity", 1.0)
	await get_tree().create_timer(0.4).timeout
	mat.set_shader_parameter("shake_intensity", 0.0)
	